<!DOCTYPE html>
<html lang="zh-Hans">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 6.2.0">


  <link rel="apple-touch-icon" sizes="180x180" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="32x32" href="/vhuichen.ico">
  <link rel="icon" type="image/png" sizes="16x16" href="/vhuichen.ico">
  <link rel="mask-icon" href="/vhuichen.ico" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css" integrity="sha256-AbA177XfpSnFEvgpYu1jMygiLabzPCJCRIBtR5jGc0k=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"vhuichen.github.io","root":"/","images":"/images","scheme":"Pisces","darkmode":false,"version":"8.13.0","exturl":false,"sidebar":{"position":"left","display":"display","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"Searching...","empty":"We didn't find any results for the search: ${query}","hits_time":"${hits} results found in ${time} ms","hits":"${hits} results found"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="前言由于某些原因，国内的经纬度坐标系并不是使用通用的经纬度坐标系。当坐标定位到国内时就会出现偏差，所以我们需要先判断经纬度坐标是否在国内，如果是，则需要先转成国内坐标。 常用坐标系WGS84坐标系国际坐标，Google地图、苹果地图使用的坐标系。GPS信号直接解析的经纬度信息也是这个坐标系的。 GCJ02坐标系中国坐标，高德地图、腾讯地图使用这个坐标系。国内基站定位、WIFI定位获取到的也是 GC">
<meta property="og:type" content="article">
<meta property="og:title" content="iOS 地图定位坐标偏差解决方案">
<meta property="og:url" content="https://vhuichen.github.io/2018/07/2018/07/iOS%20%E5%9C%B0%E5%9B%BE%E5%AE%9A%E4%BD%8D%E5%9D%90%E6%A0%87%E5%81%8F%E5%B7%AE%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/index.html">
<meta property="og:site_name" content="vhuichen">
<meta property="og:description" content="前言由于某些原因，国内的经纬度坐标系并不是使用通用的经纬度坐标系。当坐标定位到国内时就会出现偏差，所以我们需要先判断经纬度坐标是否在国内，如果是，则需要先转成国内坐标。 常用坐标系WGS84坐标系国际坐标，Google地图、苹果地图使用的坐标系。GPS信号直接解析的经纬度信息也是这个坐标系的。 GCJ02坐标系中国坐标，高德地图、腾讯地图使用这个坐标系。国内基站定位、WIFI定位获取到的也是 GC">
<meta property="og:locale">
<meta property="article:published_time" content="2018-07-08T16:00:00.000Z">
<meta property="article:modified_time" content="2018-07-17T15:35:00.694Z">
<meta property="article:author" content="vhuichen">
<meta property="article:tag" content="地图">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="https://vhuichen.github.io/2018/07/2018/07/iOS%20%E5%9C%B0%E5%9B%BE%E5%AE%9A%E4%BD%8D%E5%9D%90%E6%A0%87%E5%81%8F%E5%B7%AE%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-Hans","comments":true,"permalink":"https://vhuichen.github.io/2018/07/2018/07/iOS%20%E5%9C%B0%E5%9B%BE%E5%AE%9A%E4%BD%8D%E5%9D%90%E6%A0%87%E5%81%8F%E5%B7%AE%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/","path":"2018/07/2018/07/iOS 地图定位坐标偏差解决方案/","title":"iOS 地图定位坐标偏差解决方案"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>iOS 地图定位坐标偏差解决方案 | vhuichen</title>
  






  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="Toggle navigation bar" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">vhuichen</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">记录、分享</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section">Home</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section">Archives</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section">About</a></li>
  </ul>
</nav>




</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          Table of Contents
        </li>
        <li class="sidebar-nav-overview">
          Overview
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%89%8D%E8%A8%80"><span class="nav-number">1.</span> <span class="nav-text">前言</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B8%B8%E7%94%A8%E5%9D%90%E6%A0%87%E7%B3%BB"><span class="nav-number">1.1.</span> <span class="nav-text">常用坐标系</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#WGS84%E5%9D%90%E6%A0%87%E7%B3%BB"><span class="nav-number">1.1.1.</span> <span class="nav-text">WGS84坐标系</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#GCJ02%E5%9D%90%E6%A0%87%E7%B3%BB"><span class="nav-number">1.1.2.</span> <span class="nav-text">GCJ02坐标系</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%9B%BD%E5%86%85%E7%9A%84%E5%85%B6%E4%BB%96%E5%9D%90%E6%A0%87%E7%B3%BB"><span class="nav-number">1.1.3.</span> <span class="nav-text">国内的其他坐标系</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#iOS-%E8%8B%B9%E6%9E%9C%E5%9C%B0%E5%9B%BE%E5%BC%80%E5%8F%91"><span class="nav-number">1.2.</span> <span class="nav-text">iOS 苹果地图开发</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1%E3%80%81%E5%9B%BD%E5%AE%B6%E5%8C%BA%E5%9F%9F%E5%88%A4%E6%96%AD"><span class="nav-number">1.2.1.</span> <span class="nav-text">1、国家区域判断</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2%E3%80%81%E8%B0%83%E7%94%A8%E9%80%86%E5%9C%B0%E7%90%86%E7%BC%96%E7%A0%81%E6%8E%A5%E5%8F%A3%EF%BC%8C%E8%BF%94%E5%9B%9E%E5%9B%BD%E5%AE%B6%E7%A0%81"><span class="nav-number">1.2.2.</span> <span class="nav-text">2、调用逆地理编码接口，返回国家码</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#3%E3%80%81%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E6%9C%89%E2%80%9C%E9%AB%98%E5%BE%B7%E5%9C%B0%E5%9B%BE%E2%80%9D%E7%9A%84%E5%9B%BE%E7%89%87"><span class="nav-number">1.2.3.</span> <span class="nav-text">3、判断是否有“高德地图”的图片</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#4%E3%80%81%E4%BD%BF%E7%94%A8%E5%9B%BD%E5%86%85%E7%89%88%E5%92%8C%E5%9B%BD%E9%99%85%E7%89%88"><span class="nav-number">1.2.4.</span> <span class="nav-text">4、使用国内版和国际版</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#5%E3%80%81%E7%99%BB%E5%BD%95%E7%95%8C%E9%9D%A2%E6%B7%BB%E5%8A%A0%E9%80%89%E6%8B%A9%E6%A1%86"><span class="nav-number">1.2.5.</span> <span class="nav-text">5、登录界面添加选择框</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E7%BB%93%E6%9D%9F"><span class="nav-number">2.</span> <span class="nav-text">结束</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="vhuichen"
      src="/images/vhuichen.jpg">
  <p class="site-author-name" itemprop="name">vhuichen</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">78</span>
          <span class="site-state-item-name">posts</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">categories</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">86</span>
        <span class="site-state-item-name">tags</span></a>
      </div>
  </nav>
</div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="Back to top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-Hans">
    <link itemprop="mainEntityOfPage" href="https://vhuichen.github.io/2018/07/2018/07/iOS%20%E5%9C%B0%E5%9B%BE%E5%AE%9A%E4%BD%8D%E5%9D%90%E6%A0%87%E5%81%8F%E5%B7%AE%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/vhuichen.jpg">
      <meta itemprop="name" content="vhuichen">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="vhuichen">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="iOS 地图定位坐标偏差解决方案 | vhuichen">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          iOS 地图定位坐标偏差解决方案
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">Posted on</span>

      <time title="Created: 2018-07-09 00:00:00" itemprop="dateCreated datePublished" datetime="2018-07-09T00:00:00+08:00">2018-07-09</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">Edited on</span>
      <time title="Modified: 2018-07-17 23:35:00" itemprop="dateModified" datetime="2018-07-17T23:35:00+08:00">2018-07-17</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">In</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/iOS%E5%BC%80%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">iOS开发</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h2 id="前言"><a href="#前言" class="headerlink" title="前言"></a>前言</h2><p>由于某些原因，国内的经纬度坐标系并不是使用通用的经纬度坐标系。当坐标定位到国内时就会出现偏差，所以我们需要先判断经纬度坐标是否在国内，如果是，则需要先转成国内坐标。</p>
<h3 id="常用坐标系"><a href="#常用坐标系" class="headerlink" title="常用坐标系"></a>常用坐标系</h3><h4 id="WGS84坐标系"><a href="#WGS84坐标系" class="headerlink" title="WGS84坐标系"></a>WGS84坐标系</h4><p>国际坐标，Google地图、苹果地图使用的坐标系。GPS信号直接解析的经纬度信息也是这个坐标系的。</p>
<h4 id="GCJ02坐标系"><a href="#GCJ02坐标系" class="headerlink" title="GCJ02坐标系"></a>GCJ02坐标系</h4><p>中国坐标，高德地图、腾讯地图使用这个坐标系。国内基站定位、WIFI定位获取到的也是 GCJ02 的坐标。</p>
<h4 id="国内的其他坐标系"><a href="#国内的其他坐标系" class="headerlink" title="国内的其他坐标系"></a>国内的其他坐标系</h4><p>还有一些国内的地图，在 GCJ02 坐标的基础上再进行一次加密。例如：百度坐标（BD09）。</p>
<h3 id="iOS-苹果地图开发"><a href="#iOS-苹果地图开发" class="headerlink" title="iOS 苹果地图开发"></a>iOS 苹果地图开发</h3><p>苹果地图在国内使用的是高德地图的数据（GCJ02坐标系），在国外则使用自己的数据（WGS84坐标系）。那么我们应该怎么区分国内外呢？这里有几种方法</p>
<h4 id="1、国家区域判断"><a href="#1、国家区域判断" class="headerlink" title="1、国家区域判断"></a>1、国家区域判断</h4><p>用大量的点将将中国圈出来（港澳台地区要单独出来），然后判断经纬度是否在这个区域内。边界点越多越精确。<br>缺点：数据越多运算量越大，数据少精确度就低。</p>
<span id="more"></span>

<h4 id="2、调用逆地理编码接口，返回国家码"><a href="#2、调用逆地理编码接口，返回国家码" class="headerlink" title="2、调用逆地理编码接口，返回国家码"></a>2、调用逆地理编码接口，返回国家码</h4><figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">[[[<span class="built_in">CLGeocoder</span> alloc] init] reverseGeocodeLocation:location completionHandler:^(<span class="built_in">NSArray</span> *placemarks, <span class="built_in">NSError</span> *error) &#123;</span><br><span class="line">	<span class="keyword">if</span>(placemarks.count) &#123;</span><br><span class="line">		<span class="built_in">CLPlacemark</span> *placemark = [placemarks firstObject];</span><br><span class="line">		<span class="built_in">NSLog</span>(<span class="string">@&quot;placemark = %@ %@&quot;</span>,placemark.country, placemark.ISOcountryCode);</span><br><span class="line">	&#125;</span><br><span class="line">&#125;];</span><br></pre></td></tr></table></figure>
<p>通过上面的接口就可以返回国家码，当国家码为<code>CN</code>时就判定为国内。这里由于 GCJ02 和 WGS84 坐标的偏差在一千米内，所以在判断国家码时可以忽略。<br>缺点：请求接口会有网络延时，依赖网络。</p>
<h4 id="3、判断是否有“高德地图”的图片"><a href="#3、判断是否有“高德地图”的图片" class="headerlink" title="3、判断是否有“高德地图”的图片"></a>3、判断是否有“高德地图”的图片</h4><p>当苹果地图使用高德地图的数据时，地图上会出现 “高德地图”的字眼（其实是一张图片），我们可以通过这个来判断应该使用什么坐标系来显示。判断代码如下：  </p>
<figure class="highlight objc"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">[<span class="keyword">self</span>.mapView.subviews enumerateObjectsUsingBlock:^(__kindof <span class="built_in">UIView</span> * _Nonnull obj, <span class="built_in">NSUInteger</span> idx, <span class="type">BOOL</span> * _Nonnull stop) &#123;</span><br><span class="line">	<span class="keyword">if</span> ([obj isKindOfClass:[<span class="built_in">UIImageView</span> <span class="keyword">class</span>]] &amp;&amp; (obj.size.width == <span class="number">45.5</span>) &amp;&amp; (obj.size.height == <span class="number">10.5</span>)) &#123;</span><br><span class="line">		isAutoNavi = <span class="literal">YES</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;];</span><br></pre></td></tr></table></figure>
<p>缺点：目前是通过判断图片大小来断定是否有使用高德数据，图片大小都是（45.5 x 10.5）。可能不同手机会不同，这个有待测试。  </p>
<h4 id="4、使用国内版和国际版"><a href="#4、使用国内版和国际版" class="headerlink" title="4、使用国内版和国际版"></a>4、使用国内版和国际版</h4><p>在国内上架国内版，使用 GCJ02 坐标系。国际则使用国际版（WGS84 坐标系）。这里由于港澳台使用的是 WGS84 坐标系，所以也划分到国际版里面。<br>缺点：国内版只能在国内使用，到了国外就会有偏差，反之亦然。</p>
<h4 id="5、登录界面添加选择框"><a href="#5、登录界面添加选择框" class="headerlink" title="5、登录界面添加选择框"></a>5、登录界面添加选择框</h4><p>让用户去选择地区，类似于 Android 在登录页面让用户去选择使用高德地图还是谷歌地图。<br>缺点：UI需要改动。</p>
<h2 id="结束"><a href="#结束" class="headerlink" title="结束"></a>结束</h2><p>使用上面的几种方法都可以“解决”地图定位坐标偏差的问题。网上有个经纬度坐标转换的库，坐标系转换没问题，但判断是否在国内的误差比较大，没有分离港澳台。有兴趣可以看看，<a target="_blank" rel="noopener" href="https://github.com/JackZhouCn/JZLocationConverter">JZLocationConverter</a>。<br>目前我用这个库来实现坐标系转换，但判断是否在国内使用的是上面的方法3。虽然我们的用户不多，但从目前后台收集到的数据看来，<strong>方法2和方法3都是可以准确判断的。</strong> </p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/%E5%9C%B0%E5%9B%BE/" rel="tag"># 地图</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2018/07/2018/07/iOS%20%E5%8D%95%E4%BE%8B%E5%AE%8C%E6%95%B4%E5%86%99%E6%B3%95/" rel="prev" title="iOS 单例完整写法">
                  <i class="fa fa-chevron-left"></i> iOS 单例完整写法
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2018/07/2018/07/UITextField%20%E6%B7%BB%E5%8A%A0%E7%82%B9%E5%87%BB%E4%BA%8B%E4%BB%B6/" rel="next" title="UITextField 添加点击事件">
                  UITextField 添加点击事件 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 2015 – 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">vhuichen</span>
</div>
  <div class="powered-by">Powered by <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/pisces/" rel="noopener" target="_blank">NexT.Pisces</a>
  </div>

    </div>
  </footer>

  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script>

  





  





</body>
</html>
